package com.yy.wga.common.util;

import com.yy.wga.common.core.enmus.EUserGrade;
import com.yy.wga.common.util.common.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * @Author: gc
 * @Date: 2024/11/11
 * @Description:
 **/
public class CommissionUtil {

    private static final BigDecimal COMMISSION_ALL_RADIO = new BigDecimal("1000000");
    private static final BigDecimal USER_ALL_RADIO = new BigDecimal("100");

    private CommissionUtil() {
    }

    /**
     * 获取佣金
     *
     * @param goodsPrice     商品价格
     * @param commissionRate 分佣类型值 百分制
     * @return
     */
    public static Integer getCommission(Integer goodsPrice, BigDecimal commissionRate) {
        BigDecimal priceDecimal = new BigDecimal(goodsPrice).divide(new BigDecimal("100"), 2, RoundingMode.DOWN);
        // 平台保留10% + 6%的手续费
        priceDecimal = priceDecimal.multiply(new BigDecimal("0.84")).setScale(2, RoundingMode.DOWN);
        BigDecimal commission = priceDecimal.multiply(commissionRate.divide(new BigDecimal("100"), 2, RoundingMode.DOWN));
        return NumberUtils.changeY2F(commission.doubleValue());
    }

    public static Integer getCommission(BigDecimal goodsPrice, BigDecimal commissionRate) {
        // 平台保留10% + 6%的手续费
        goodsPrice = goodsPrice.multiply(new BigDecimal("0.84")).setScale(2, RoundingMode.DOWN);
        BigDecimal commission = goodsPrice.multiply(commissionRate.divide(new BigDecimal("100"), 2, RoundingMode.DOWN));
        return NumberUtils.changeY2F(commission.doubleValue());
    }

    /**
     * 获取佣金
     *
     * @param commissionValue 佣金
     * @param commissionValue 直推佣金比例 百分制
     * @return
     */
    public static Integer getDirectCommission(Integer commissionValue, BigDecimal commissionRate) {
        BigDecimal priceDecimal = new BigDecimal(commissionValue).divide(new BigDecimal("100"), 2, RoundingMode.DOWN);
        BigDecimal commission = priceDecimal.multiply(commissionRate.divide(new BigDecimal("100"), 2, RoundingMode.DOWN));
        return NumberUtils.changeY2F(commission.doubleValue());
    }

    public static Integer calcCommissionValue(Integer salePrice, Integer commissionRatio, EUserGrade userGrade) {
        if (Objects.isNull(commissionRatio)) {
            commissionRatio = 0;
        }
        // 佣金比例 43%
        BigDecimal commonRatio = BigDecimal.valueOf(commissionRatio);
        // 用户等级比例 40%
        BigDecimal userGradeRadio = calcUserGradeRadio(userGrade);
        // 订单金额*0.84(平台保留10% + 6%的手续费) *服务商佣金比例*用户等级佣金比例  / 服务商总比例*用户总佣金比例
        BigDecimal calcCommissionValue = BigDecimal.valueOf(Long.valueOf(salePrice)).multiply(new BigDecimal("0.84")).multiply(commonRatio).multiply(userGradeRadio).divide(COMMISSION_ALL_RADIO.multiply(USER_ALL_RADIO), 2, RoundingMode.DOWN);
        return calcCommissionValue.intValue();
    }

    public static Integer calcCommissionValue(Integer originCommissionValue, EUserGrade userGrade) {
        // 用户等级比例 40%
        BigDecimal userGradeRadio = calcUserGradeRadio(userGrade);
        // 原始佣金 *用户等级佣金比例  / * 用户总佣金比例
        BigDecimal calcCommissionValue = BigDecimal.valueOf(originCommissionValue).multiply(userGradeRadio).divide(USER_ALL_RADIO, 2, RoundingMode.DOWN);
        return calcCommissionValue.intValue();
    }

    /**
     * 计算用户等级佣金
     * @param originCommissionValue 原始佣金
     * @param userGrade 用户等级
     * @return 计算后的佣金
     */
    public static Integer getUserGradeCommission(Integer originCommissionValue, EUserGrade userGrade) {
        // 用户等级比例 40%
        BigDecimal userGradeRadio = calcUserGradeRadio(userGrade);
        return getCommission(originCommissionValue,userGradeRadio);
    }

    /**
     * 计算用户累积 自购/分销分佣比例
     * @param userGrade 用户等级
     * @return 比例
     */
    public static BigDecimal calcUserGradeRadio(EUserGrade userGrade) {
        if (Objects.isNull(userGrade)) {
            userGrade = EUserGrade.COMMON;
        }
        // 用户等级比例
        List<EUserGrade> commissionLinks = EUserGrade.getCommissionLink();
        // 用户等级比例 40%
        BigDecimal userGradeRadio = BigDecimal.ZERO;
        if (commissionLinks.contains(userGrade)) {
            for (EUserGrade commissionLink : commissionLinks) {
                userGradeRadio = userGradeRadio.add(commissionLink.getBuyCommissionRate());
                if (Objects.equals(commissionLink, userGrade)) {
                    break;
                }
            }
        }
        return userGradeRadio;
    }
}
